// import utils
import apiRequest from "./request";
import store from "@/store/index";

/* --------------------------------------------------------------
 *                          User API
 * -------------------------------------------------------------- */

/**
 * User login API function
 * @param {Object} loginData 
 * @returns {Promise}
 */
async function userLogin(loginData) {
    try {
        const response = await apiRequest('post', '/login/login', loginData);

        if (response.code === 200 && response.success) {
            // Get token from response data/  
            const token = response.data.token;

            // Store the token  in Vuex
            store.commit('user/setToken', token);
            return { success: true};
        } else {
            throw new Error('用户登录失败');
        }
    } catch (error) {
        console.error('用户登录失败:', error);
        throw new Error('用户登录失败');
    }
}

/**
 * Uer logout API function
 * @returns {void}
 */
function userLogout() {
    try {
        // Delete token from store
        store.commit('user/delToken');
        return { success: true};
    } catch (error) {
        console.error('用户退出登录失败:', error);
        throw new Error('用户退出登录失败');
    }
}

export { userLogin, userLogout };